<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input v-model="listQuery.name" placeholder="知识名称" style="width: 200px;" class="filter-item" @keyup.enter.native="handleFilter" />
      <el-select v-model="listQuery.isDel" placeholder="是否有效" clearable style="width: 90px" class="filter-item">
        <el-option label="全部" value="" />
        <el-option label="有效" value="1" />
        <el-option label="失效" value="0" />
      </el-select>
      <el-button class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">
        Search
      </el-button>
      <el-button class="filter-item" style="margin-left: 10px;" type="primary" icon="el-icon-edit" @click="handleCreate">
        Add
      </el-button>
    </div>
    <el-table
      :data="list"
      border
      fit
      highlight-current-row
      style="width: 100%;"
    >
      <el-table-column label="ID" prop="id" width="80" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.id }}</span>
        </template>
      </el-table-column>
      <el-table-column label="Name" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.name }}</span>
        </template>
      </el-table-column>
      <el-table-column label="type" align="center">
        <template slot-scope="scope">
          <span>{{ typelabel(scope.row.type) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="State" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.isDel==1?'有效':'失效' }}</span>
        </template>
      </el-table-column>
      <el-table-column label="Actions" width="250px" align="center" class-name="small-padding fixed-width">
        <template slot-scope="{row}">
          <template v-if="row.isDel==1">
            <el-button type="primary" size="mini" @click="handleDetails(row)">
              Details
            </el-button>
            <el-button type="primary" size="mini" @click="handleUpdate(row)">
              Edit
            </el-button>
            <el-button size="mini" type="danger" @click="handleModifyStatus(row.id, 0)">
              Delete
            </el-button>
          </template>
          <el-button v-else size="mini" type="info" @click="handleModifyStatus(row.id, 1)">
            recovery
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total>0" :total="total" :page.sync="listQuery.pageNo" :limit.sync="listQuery.pageSize" @pagination="getList" />
  </div>
</template>

<script>
import Pagination from '@/components/Pagination' // secondary package based on el-pagination
export default {
  name: 'Case',
  components: { Pagination },
  data() {
    return {
      list: null,
      total: 0,
      listLoading: true,
      listQuery: {
        pageNo: 1,
        pageSize: 10,
        name: '',
        isDel: ''
      },
      editId: undefined,
      dialogFormVisible: false,
      dialogStatus: '',
      dialogPvVisible: false
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList() {
      this.get('/neueducase/list', response => {
        this.list = response.data.records
        this.total = response.data.total
      }, this.listQuery)
    },
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    handleModifyStatus(id, status) {
      const txt = status === 0 ? '确定要删除该数据吗?' : '确定要恢复该数据吗?'
      this.del('/neueducase/del', result => {
        this.getList()
      }, { id: id, isDel: status }, txt)
    },
    handleCreate() {
      this.redirect('caseEdit')
    },
    handleUpdate(row) {
      this.redirect('caseEdit', {
        id: row.id
      })
    },
    handleDetails(row) {
      this.redirect('CaseDetail', {
        id: row.id
      })
    },
    typelabel(typeid) {
      for (let i = 0; i < this.$store.getters.casetype.length; i++) {
        if (typeid === this.$store.getters.casetype[i].value) {
          return this.$store.getters.casetype[i].label
        }
      }
    }
  }
}
</script>
<style scoped lang="scss">
  .filter-container{
    padding-bottom: 10px;
  }
</style>
